﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;

namespace Waiter.View
{
    public partial class FrmMenuItems : DevComponents.DotNetBar.Office2007Form
    {
        public FrmMenuItems()
        {
            InitializeComponent();
        }

        private void headerItem1_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.headerItem1_Load", "", "");
            this.headerItem1.init("DANH SÁCH MÓN", false);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.btnClose_Click", "", "");
            this.Close();
        }

        bool addFlag = CommonConstants.FLAG_OFF;
        private void btnAdd_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.btnAdd_Click", "", "");
            addFlag = CommonConstants.FLAG_ON;
            this.txtDrinkName.Text = "";
            this.txtPrice.Value = 0;
            CommonCtrl.fsCmbSetSelectedIndex(this.cmbGroup, 0);
            CommonCtrl.fsCmbSetSelectedIndex(this.cmbStatus, 0);
            CommonCtrl.fsCmbSetSelectedIndex(this.cmbUnit, 0);
            this.txtDrinkName.Focus();
        }

        private void btnAddGroup_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.btnAddGroup_Click", "", "");
            frmGroupList frm = new frmGroupList(CommonConstants.NHOM_MON);
            frm.ShowDialog();
            MasterCtrl masterCtrl = new MasterCtrl();
            masterCtrl.fsLoadCmbGroupList(this.cmbGroup, CommonConstants.NHOM_MON, null);
        }

        private void btnAddUnit_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.btnAddUnit_Click", "", "");
            frmGroupList frm = new frmGroupList(CommonConstants.DON_VI_TINH);
            frm.ShowDialog();
            MasterCtrl masterCtrl = new MasterCtrl();
            masterCtrl.fsLoadCmbGroupList(this.cmbUnit, CommonConstants.DON_VI_TINH, null);
        }

        private void FrmMenuItems_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.FrmMenuItems_Load", "", "");
            loaddingFlg = CommonConstants.FLAG_ON;
            MasterCtrl masterCtrl = new MasterCtrl();
            masterCtrl.fsLoadCmbGroupList(this.cmbGroup, CommonConstants.NHOM_MON, null);
            masterCtrl.fsLoadCmbGroupList(this.cmbUnit, CommonConstants.DON_VI_TINH, null);
            fsLoadCmbStatus();
            fsLoadDgrMenuList();
            loaddingFlg = CommonConstants.FLAG_OFF;
            fsLoadMenuInfo();
        }

        private void fsLoadCmbStatus()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.fsLoadCmbStatus", "", "");
            List<ComboboxItem> items = new List<ComboboxItem>();
            ComboboxItem item1 = new ComboboxItem();
            item1.Value = ((int)MenuStatus.AVAILABLE).ToString();
            item1.Text = "Còn hàng";

            ComboboxItem item2 = new ComboboxItem();
            item2.Value = ((int)MenuStatus.NOT_AVAILABLE).ToString();
            item2.Text = "Hết hàng";

            items.Add(item1);
            items.Add(item2);

            this.cmbStatus.DataSource = items;
            this.cmbStatus.DisplayMember = CommonConstants.CMB_TEXT;
            this.cmbStatus.ValueMember = CommonConstants.CMB_VALUE;
            CommonCtrl.fsCmbSetSelectedIndex(this.cmbStatus, 0);
        }

        private void fsLoadDgrMenuList()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.fsLoadDgrMenuList", "", "");
            this.dgrMenu.Rows.Clear();

            MasterCtrl masterCtrl = new MasterCtrl();
            MenuCtrl menuCtrl = new MenuCtrl();
            List<MENU> menuList = menuCtrl.fsGetMenuList();
            if (menuList == null || menuList.Count == 0)
            {
                return;
            }

            this.dgrMenu.Rows.Add(menuList.Count);
            for (int i = 0; i < menuList.Count; i++)
            {
                this.dgrMenu.Rows[i].Cells["colCode"].Value = menuList[i].CODE;
                this.dgrMenu.Rows[i].Cells["colDrinkName"].Value = menuList[i].DRINKS_NAME;
                this.dgrMenu.Rows[i].Cells["colGroupCode"].Value = menuList[i].CODE_GROUP;
                this.dgrMenu.Rows[i].Cells["colGroupName"].Value = masterCtrl.
                    fsGetMasterFromGroupCode(CommonConstants.NHOM_MON, menuList[i].CODE_GROUP).VALUE;
                this.dgrMenu.Rows[i].Cells["colStatusCode"].Value = menuList[i].CODE_STATUS;
                this.dgrMenu.Rows[i].Cells["colStatusName"].Value = fsGetStatusName(menuList[i].CODE_STATUS);
                this.dgrMenu.Rows[i].Cells["colUnitCode"].Value = menuList[i].UNIT_CODE;
                this.dgrMenu.Rows[i].Cells["colUnitName"].Value = masterCtrl.
                    fsGetMasterFromGroupCode(CommonConstants.DON_VI_TINH, menuList[i].UNIT_CODE).VALUE;
                this.dgrMenu.Rows[i].Cells["colPrice"].Value = menuList[i].UNIT_PRICE;
            }
        }

        private string fsGetStatusName(int status)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.fsGetStatusName", "", "");
            switch (status)
            {
                case (int)MenuStatus.AVAILABLE:
                    return "Còn hàng";
                case (int)MenuStatus.NOT_AVAILABLE:
                    return "Hết hàng";
            }
            return "";
        }

        bool loaddingFlg = CommonConstants.FLAG_ON;

        private void fsLoadMenuInfo()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.fsLoadMenuInfo", "", "");
            try
            {
                if (this.loaddingFlg)
                {
                    return;
                }

                if (this.dgrMenu.Rows.Count == 0)
                {
                    return;
                }

                this.txtDrinkName.Text = this.dgrMenu.CurrentRow.Cells["colDrinkName"].Value.ToString();

                string groupCode = this.dgrMenu.CurrentRow.Cells["colGroupCode"].Value.ToString();
                CommonCtrl.fsCmbSetSelectedValue(this.cmbGroup, groupCode);

                string unitCode = this.dgrMenu.CurrentRow.Cells["colUnitCode"].Value.ToString();
                CommonCtrl.fsCmbSetSelectedValue(this.cmbUnit, unitCode);

                this.txtPrice.Text = this.dgrMenu.CurrentRow.Cells["colPrice"].Value.ToString();

                string statusCode = this.dgrMenu.CurrentRow.Cells["colStatusCode"].Value.ToString();
                CommonCtrl.fsCmbSetSelectedValue(this.cmbStatus, statusCode);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.fsLoadMenuInfo", "", ex.ToString());
            }
        }

        private void dgrMenu_SelectionChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.dgrMenu_SelectionChanged", "", "");
            if (loaddingFlg)
            {
                return;
            }
            fsLoadMenuInfo();
            addFlag = CommonConstants.FLAG_OFF;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.btnDelete_Click", "", "");
            if (this.dgrMenu.Rows.Count == 0)
            {
                return;
            }

            DialogResult dls = DevComponents.DotNetBar.MessageBoxEx.Show("Bạn có chắc muốn xóa không?!", CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dls != System.Windows.Forms.DialogResult.Yes)
            {
                return;
            }

            MenuCtrl menuCtrl = new MenuCtrl();
            int idx = this.dgrMenu.CurrentRow.Index;
            string menuCode = this.dgrMenu.Rows[idx].Cells["colCode"].Value.ToString();
            if (menuCtrl.fsDeleteMenu(menuCode) != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.dgrMenu.Rows.RemoveAt(idx);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmMenuItems.btnSave_Click", "", "");
            MenuCtrl menuCtrl = new MenuCtrl();
            string menuName = this.txtDrinkName.Text.Trim();
            if (menuName == string.Empty)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn phải nhập tên món trước!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtDrinkName.Focus();
                return;
            }

            if (this.txtPrice.Value == 0)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Gía bán phải lớn hơn 0!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtPrice.Focus();
                return;
            }

            MENU item = new MENU();
            item.DRINKS_NAME = menuName;
            item.DRINKS_NAME_SUB = "";
            item.CODE_GROUP = CommonCtrl.fsCmbGetSelectedValue(this.cmbGroup);
            item.CODE_STATUS = Convert.ToInt32(CommonCtrl.fsCmbGetSelectedValue(this.cmbStatus));
            item.UNIT_CODE = CommonCtrl.fsCmbGetSelectedValue(this.cmbUnit);
            item.OPID = CommonUtils.fsGetOPID();
            item.DEL_FLG = CommonConstants.FLAG_OFF;
            item.UNIT_PRICE = this.txtPrice.Value;

            int idx = 0;
            if (addFlag)
            {
                if (menuCtrl.fsIsExistMenu(item.DRINKS_NAME, string.Empty))
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Món này đã có rồi!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.txtDrinkName.SelectAll();
                    this.txtDrinkName.Focus();
                    return;
                }

                item.CODE = CommonUtils.fsCreateNewCode();
                item.ADD_DATE = DateTime.Now;

                if (menuCtrl.fsInsertMenu(item) != ResultStatus.SUCCESS)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.dgrMenu.Rows.Add(1);
                idx = this.dgrMenu.Rows.Count - 1;
                this.dgrMenu.Rows[idx].Cells["colCode"].Value = item.CODE;
            }
            else
            {
                if (this.dgrMenu.Rows.Count == 0)
                {
                    return;
                }
                idx = this.dgrMenu.CurrentRow.Index;
                item.CODE = this.dgrMenu.Rows[idx].Cells["colCode"].Value.ToString();
                if (menuCtrl.fsIsExistMenu(item.DRINKS_NAME, item.CODE))
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Món này đã có rồi!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.txtDrinkName.SelectAll();
                    this.txtDrinkName.Focus();
                    return;
                }
                item.UPD_DATE = DateTime.Now;
                if (menuCtrl.fsUpdateMenu(item) != ResultStatus.SUCCESS)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            this.dgrMenu.Rows[idx].Cells["colDrinkName"].Value = item.DRINKS_NAME;
            this.dgrMenu.Rows[idx].Cells["colGroupCode"].Value = item.CODE_GROUP;
            this.dgrMenu.Rows[idx].Cells["colGroupName"].Value = CommonCtrl.fsCmbGetSelectedText(this.cmbGroup);
            this.dgrMenu.Rows[idx].Cells["colStatusCode"].Value = item.CODE_STATUS;
            this.dgrMenu.Rows[idx].Cells["colStatusName"].Value = fsGetStatusName(item.CODE_STATUS);
            this.dgrMenu.Rows[idx].Cells["colUnitCode"].Value = item.UNIT_CODE;
            this.dgrMenu.Rows[idx].Cells["colUnitName"].Value = CommonCtrl.fsCmbGetSelectedText(this.cmbUnit);
            this.dgrMenu.Rows[idx].Cells["colPrice"].Value = item.UNIT_PRICE;
            CommonCtrl.fsDgrSetSelectedRow(this.dgrMenu, idx, 1);

            DevComponents.DotNetBar.MessageBoxEx.Show("Đã cập nhật xong!", CommonMessages.MSG_NOTICE_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
